home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / stl_threads.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  12.4 KB  |  375 lines

  1. /*
  2.  * Copyright (c) 1997-1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Silicon Graphics makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. // WARNING: This is an internal header file, included by other C++
  15. // standard library headers.  You should not attempt to use this header
  16. // file directly.
  17. // Stl_config.h should be included before this file.
  18.  
  19. #ifndef __SGI_STL_INTERNAL_THREADS_H
  20. #define __SGI_STL_INTERNAL_THREADS_H
  21.  
  22. // Supported threading models are native SGI, pthreads, uithreads
  23. // (similar to pthreads, but based on an earlier draft of the Posix
  24. // threads standard), and Win32 threads.  Uithread support by Jochen
  25. // Schlick, 1999.
  26.  
  27. #if defined(__STL_SGI_THREADS)
  28. #include <mutex.h>
  29. #include <time.h>
  30. #elif defined(__STL_PTHREADS)
  31. #include <pthread.h>
  32. #elif defined(__STL_UITHREADS)
  33. #include <thread.h>
  34. #include <synch.h>
  35. #elif defined(__STL_WIN32THREADS)
  36. #include <windows.h>
  37. #endif
  38.  
  39. __STL_BEGIN_NAMESPACE
  40.  
  41.  
  42. // Class _Refcount_Base provides a type, _RC_t, a data member,
  43. // _M_ref_count, and member functions _M_incr and _M_decr, which perform
  44. // atomic preincrement/predecrement.  The constructor initializes 
  45. // _M_ref_count.
  46.  
  47. // Hack for SGI o32 compilers.
  48. #if defined(__STL_SGI_THREADS) && !defined(__add_and_fetch) && \
  49.     (__mips < 3 || !(defined (_ABIN32) || defined(_ABI64)))
  50. #  define __add_and_fetch(__l,__v) add_then_test((unsigned long*)__l,__v)  
  51. #  define __test_and_set(__l,__v)  test_and_set(__l,__v)
  52. #endif /* o32 */
  53.  
  54. struct _Refcount_Base
  55. {
  56.   // The type _RC_t
  57. # ifdef __STL_WIN32THREADS
  58.   typedef long _RC_t;
  59. # else
  60.   typedef size_t _RC_t;
  61. #endif
  62.   
  63.   // The data member _M_ref_count
  64.    volatile _RC_t _M_ref_count;
  65.  
  66.   // Constructor
  67. # ifdef __STL_PTHREADS
  68.   pthread_mutex_t _M_ref_count_lock;
  69.   _Refcount_Base(_RC_t __n) : _M_ref_count(__n)
  70.     { pthread_mutex_init(&_M_ref_count_lock, 0); }
  71. # elif defined(__STL_UITHREADS)
  72.   mutex_t         _M_ref_count_lock;
  73.   _Refcount_Base(_RC_t __n) : _M_ref_count(__n)
  74.     { mutex_init(&_M_ref_count_lock, USYNC_THREAD, 0); }
  75. # else
  76.   _Refcount_Base(_RC_t __n) : _M_ref_count(__n) {}
  77. # endif
  78.  
  79.   // _M_incr and _M_decr
  80. # ifdef __STL_SGI_THREADS
  81.   void _M_incr() {  __add_and_fetch(&_M_ref_count, 1); }
  82.   _RC_t _M_decr() { return __add_and_fetch(&_M_ref_count, (size_t) -1); }
  83. # elif defined (__STL_WIN32THREADS)
  84.    void _M_incr() { InterlockedIncrement((_RC_t*)&_M_ref_count); }
  85.   _RC_t _M_decr() { return InterlockedDecrement((_RC_t*)&_M_ref_count); }
  86. # elif defined(__STL_PTHREADS)
  87.   void _M_incr() {
  88.     pthread_mutex_lock(&_M_ref_count_lock);
  89.     ++_M_ref_count;
  90.     pthread_mutex_unlock(&_M_ref_count_lock);
  91.   }
  92.   _RC_t _M_decr() {
  93.     pthread_mutex_lock(&_M_ref_count_lock);
  94.     volatile _RC_t __tmp = --_M_ref_count;
  95.     pthread_mutex_unlock(&_M_ref_count_lock);
  96.     return __tmp;
  97.   }
  98. # elif defined(__STL_UITHREADS)
  99.   void _M_incr() {
  100.     mutex_lock(&_M_ref_count_lock);
  101.     ++_M_ref_count;
  102.     mutex_unlock(&_M_ref_count_lock);
  103.   }
  104.   _RC_t _M_decr() {
  105.     mutex_lock(&_M_ref_count_lock);
  106.     /*volatile*/ _RC_t __tmp = --_M_ref_count;
  107.     mutex_unlock(&_M_ref_count_lock);
  108.     return __tmp;
  109.   }
  110. # else  /* No threads */
  111.   void _M_incr() { ++_M_ref_count; }
  112.   _RC_t _M_decr() { return --_M_ref_count; }
  113. # endif
  114. };
  115.  
  116. // Atomic swap on unsigned long
  117. // This is guaranteed to behave as though it were atomic only if all
  118. // possibly concurrent updates use _Atomic_swap.
  119. // In some cases the operation is emulated with a lock.
  120. # ifdef __STL_SGI_THREADS
  121.     inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q) {
  122. #       if __mips < 3 || !(defined (_ABIN32) || defined(_ABI64))
  123.             return test_and_set(__p, __q);
  124. #       else
  125.             return __test_and_set(__p, (unsigned long)__q);
  126. #       endif
  127.     }
  128. # elif defined(__STL_WIN32THREADS)
  129.     inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q) {
  130.         return (unsigned long) InterlockedExchange((LPLONG)__p, (LONG)__q);
  131.     }
  132. # elif defined(__STL_PTHREADS)
  133.     // We use a template here only to get a unique initialized instance.
  134.     template<int __dummy>
  135.     struct _Swap_lock_struct {
  136.         static pthread_mutex_t _S_swap_lock;
  137.     };
  138.  
  139.     template<int __dummy>
  140.     pthread_mutex_t
  141.     _Swap_lock_struct<__dummy>::_S_swap_lock = PTHREAD_MUTEX_INITIALIZER;
  142.  
  143.     // This should be portable, but performance is expected
  144.     // to be quite awful.  This really needs platform specific
  145.     // code.
  146.     inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q) {
  147.         pthread_mutex_lock(&_Swap_lock_struct<0>::_S_swap_lock);
  148.         unsigned long __result = *__p;
  149.         *__p = __q;
  150.         pthread_mutex_unlock(&_Swap_lock_struct<0>::_S_swap_lock);
  151.         return __result;
  152.     }
  153. # elif defined(__STL_UITHREADS)
  154.     // We use a template here only to get a unique initialized instance.
  155.     template<int __dummy>
  156.     struct _Swap_lock_struct {
  157.         static mutex_t _S_swap_lock;
  158.     };
  159.  
  160.     template<int __dummy>
  161.     mutex_t
  162.     _Swap_lock_struct<__dummy>::_S_swap_lock = DEFAULTMUTEX;
  163.  
  164.     // This should be portable, but performance is expected
  165.     // to be quite awful.  This really needs platform specific
  166.     // code.
  167.     inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q) {
  168.         mutex_lock(&_Swap_lock_struct<0>::_S_swap_lock);
  169.         unsigned long __result = *__p;
  170.         *__p = __q;
  171.         mutex_unlock(&_Swap_lock_struct<0>::_S_swap_lock);
  172.         return __result;
  173.     }
  174. # elif defined (__STL_SOLARIS_THREADS)
  175.     // any better solutions ?
  176.     // We use a template here only to get a unique initialized instance.
  177.     template<int __dummy>
  178.     struct _Swap_lock_struct {
  179.         static mutex_t _S_swap_lock;
  180.     };
  181.  
  182. # if ( __STL_STATIC_TEMPLATE_DATA > 0 )
  183.     template<int __dummy>
  184.     mutex_t
  185.     _Swap_lock_struct<__dummy>::_S_swap_lock = DEFAULTMUTEX;
  186. #  else
  187.     __DECLARE_INSTANCE(mutex_t, _Swap_lock_struct<__dummy>::_S_swap_lock, 
  188.                        =DEFAULTMUTEX);
  189. # endif /* ( __STL_STATIC_TEMPLATE_DATA > 0 ) */
  190.  
  191.     // This should be portable, but performance is expected
  192.     // to be quite awful.  This really needs platform specific
  193.     // code.
  194.     inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q) {
  195.         mutex_lock(&_Swap_lock_struct<0>::_S_swap_lock);
  196.         unsigned long __result = *__p;
  197.         *__p = __q;
  198.         mutex_unlock(&_Swap_lock_struct<0>::_S_swap_lock);
  199.         return __result;
  200.     }
  201. # else
  202.     static inline unsigned long _Atomic_swap(unsigned long * __p, unsigned long __q) {
  203.         unsigned long __result = *__p;
  204.         *__p = __q;
  205.         return __result;
  206.     }
  207. # endif
  208.  
  209. // Locking class.  Note that this class *does not have a constructor*.
  210. // It must be initialized either statically, with __STL_MUTEX_INITIALIZER,
  211. // or dynamically, by explicitly calling the _M_initialize member function.
  212. // (This is similar to the ways that a pthreads mutex can be initialized.)
  213. // There are explicit member functions for acquiring and releasing the lock.
  214.  
  215. // There is no constructor because static initialization is essential for
  216. // some uses, and only a class aggregate (see section 8.5.1 of the C++
  217. // standard) can be initialized that way.  That means we must have no
  218. // constructors, no base classes, no virtual functions, and no private or
  219. // protected members.
  220.  
  221. // Helper struct.  This is a workaround for various compilers that don't
  222. // handle static variables in inline functions properly.
  223. template <int __inst>
  224. struct _STL_mutex_spin {
  225.   enum { __low_max = 30, __high_max = 1000 };
  226.   // Low if we suspect uniprocessor, high for multiprocessor.
  227.  
  228.   static unsigned __max;
  229.   static unsigned __last;
  230. };
  231.  
  232. template <int __inst>
  233. unsigned _STL_mutex_spin<__inst>::__max = _STL_mutex_spin<__inst>::__low_max;
  234.  
  235. template <int __inst>
  236. unsigned _STL_mutex_spin<__inst>::__last = 0;
  237.  
  238. struct _STL_mutex_lock
  239. {
  240. #if defined(__STL_SGI_THREADS) || defined(__STL_WIN32THREADS)
  241.   // It should be relatively easy to get this to work on any modern Unix.
  242.   volatile unsigned long _M_lock;
  243.   void _M_initialize() { _M_lock = 0; }
  244.   static void _S_nsec_sleep(int __log_nsec) {
  245. #     ifdef __STL_SGI_THREADS
  246.           struct timespec __ts;
  247.           /* Max sleep is 2**27nsec ~ 60msec      */
  248.           __ts.tv_sec = 0;
  249.           __ts.tv_nsec = 1 << __log_nsec;
  250.           nanosleep(&__ts, 0);
  251. #     elif defined(__STL_WIN32THREADS)
  252.           if (__log_nsec <= 20) {
  253.               Sleep(0);
  254.           } else {
  255.               Sleep(1 << (__log_nsec - 20));
  256.           }
  257. #     else
  258. #       error unimplemented
  259. #     endif
  260.   }
  261.   void _M_acquire_lock() {
  262.     volatile unsigned long* __lock = &this->_M_lock;
  263.  
  264.     if (!_Atomic_swap((unsigned long*)__lock, 1)) {
  265.       return;
  266.     }
  267.     unsigned __my_spin_max = _STL_mutex_spin<0>::__max;
  268.     unsigned __my_last_spins = _STL_mutex_spin<0>::__last;
  269.     volatile unsigned __junk = 17;      // Value doesn't matter.
  270.     unsigned __i;
  271.     for (__i = 0; __i < __my_spin_max; __i++) {
  272.       if (__i < __my_last_spins/2 || *__lock) {
  273.         __junk *= __junk; __junk *= __junk;
  274.         __junk *= __junk; __junk *= __junk;
  275.         continue;
  276.       }
  277.       if (!_Atomic_swap((unsigned long*)__lock, 1)) {
  278.         // got it!
  279.         // Spinning worked.  Thus we're probably not being scheduled
  280.         // against the other process with which we were contending.
  281.         // Thus it makes sense to spin longer the next time.
  282.         _STL_mutex_spin<0>::__last = __i;
  283.         _STL_mutex_spin<0>::__max = _STL_mutex_spin<0>::__high_max;
  284.         return;
  285.       }
  286.     }
  287.     // We are probably being scheduled against the other process.  Sleep.
  288.     _STL_mutex_spin<0>::__max = _STL_mutex_spin<0>::__low_max;
  289.     for (__i = 0 ;; ++__i) {
  290.       int __log_nsec = __i + 6;
  291.  
  292.       if (__log_nsec > 27) __log_nsec = 27;
  293.       if (!_Atomic_swap((unsigned long *)__lock, 1)) {
  294.         return;
  295.       }
  296.       _S_nsec_sleep(__log_nsec);
  297.     }
  298.   }
  299.   void _M_release_lock() {
  300.     volatile unsigned long* __lock = &_M_lock;
  301. #   if defined(__STL_SGI_THREADS) && defined(__GNUC__) && __mips >= 3
  302.         asm("sync");
  303.         *__lock = 0;
  304. #   elif defined(__STL_SGI_THREADS) && __mips >= 3 \
  305.          && (defined (_ABIN32) || defined(_ABI64))
  306.         __lock_release(__lock);
  307. #   else 
  308.         *__lock = 0;
  309.         // This is not sufficient on many multiprocessors, since
  310.         // writes to protected variables and the lock may be reordered.
  311. #   endif
  312.   }
  313.  
  314. // We no longer use win32 critical sections.
  315. // They appear to be slower in the contention-free case,
  316. // and they appear difficult to initialize without introducing a race.
  317.  
  318. #elif defined(__STL_PTHREADS)
  319.   pthread_mutex_t _M_lock;
  320.   void _M_initialize()   { pthread_mutex_init(&_M_lock, NULL); }
  321.   void _M_acquire_lock() { pthread_mutex_lock(&_M_lock); }
  322.   void _M_release_lock() { pthread_mutex_unlock(&_M_lock); }
  323. #elif defined(__STL_UITHREADS)
  324.   mutex_t _M_lock;
  325.   void _M_initialize()   { mutex_init(&_M_lock, USYNC_THREAD, 0); }
  326.   void _M_acquire_lock() { mutex_lock(&_M_lock); }
  327.   void _M_release_lock() { mutex_unlock(&_M_lock); }
  328. #else /* No threads */
  329.   void _M_initialize()   {}
  330.   void _M_acquire_lock() {}
  331.   void _M_release_lock() {}
  332. #endif
  333. };
  334.  
  335. #ifdef __STL_PTHREADS
  336. // Pthreads locks must be statically initialized to something other than
  337. // the default value of zero.
  338. #   define __STL_MUTEX_INITIALIZER = { PTHREAD_MUTEX_INITIALIZER }
  339. #elif defined(__STL_UITHREADS)
  340. // UIthreads locks must be statically initialized to something other than
  341. // the default value of zero.
  342. #   define __STL_MUTEX_INITIALIZER = { DEFAULTMUTEX }
  343. #elif defined(__STL_SGI_THREADS) || defined(__STL_WIN32THREADS)
  344. #   define __STL_MUTEX_INITIALIZER = { 0 }
  345. #else
  346. #   define __STL_MUTEX_INITIALIZER
  347. #endif
  348.  
  349.  
  350. // A locking class that uses _STL_mutex_lock.  The constructor takes a
  351. // reference to an _STL_mutex_lock, and acquires a lock.  The
  352. // destructor releases the lock.  It's not clear that this is exactly
  353. // the right functionality.  It will probably change in the future.
  354.  
  355. struct _STL_auto_lock
  356. {
  357.   _STL_mutex_lock& _M_lock;
  358.   
  359.   _STL_auto_lock(_STL_mutex_lock& __lock) : _M_lock(__lock)
  360.     { _M_lock._M_acquire_lock(); }
  361.   ~_STL_auto_lock() { _M_lock._M_release_lock(); }
  362.  
  363. private:
  364.   void operator=(const _STL_auto_lock&);
  365.   _STL_auto_lock(const _STL_auto_lock&);
  366. };
  367.  
  368. __STL_END_NAMESPACE
  369.  
  370. #endif /* __SGI_STL_INTERNAL_THREADS_H */
  371.  
  372. // Local Variables:
  373. // mode:C++
  374. // End:
  375.